#!/usr/bin/ruby
#
# Simplebeat Copyright (C) 2007 Leroy van Logchem
#                               <leroy.vanlogchem@gmail.com>
#
# This file is part of Simplebeat
#
# Simplebeat is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# Simplebeat is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#

# Lets add our library directory to Ruby's search path
$: << File.expand_path(File.dirname(__FILE__) + "/lib")

require 'socket'
require 'init.rb'

$udp_out     = UDPSocket.new

Syslog.info("-----------------------------------------------------")
Syslog.info("Simplebeat.master starts")
Syslog.info("Beating #{@udp_freq} udp/s to:")
@remote_hosts.each do |host|
  Syslog.info("#{host}")
end 
Syslog.info("-----------------------------------------------------")


def udp_send(host)
  @udp_range.first.upto(@udp_range.last) { |port|
    $udp_out.send('Simplebeat', 0, host, port)
  }
end

fork do
  daemonize
  begin
  loop do
    sleep(1.0/@udp_freq.to_f)
    @remote_hosts.each do |host|
      udp_send(host)
    end
  end
  ensure
    Syslog.info("-----------------------------------------------------")
    Syslog.info("Simplebeat.master ends")
    Syslog.info("-----------------------------------------------------")
  end

end

puts "Started daemonized Simplebeat.master"
